package Homework;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
/*统计任意文件夹中每种文件的个数并打印.
包含子文件夹.
统计d:\\
Txt：3个
Doc:10个
Jpg：5个
DBF:2个
等等*/
public class work03 {
    public static void main(String[] args) {
        File file = new File("d:\\大三课件（上）");

        HashMap<String,Integer> hm = new HashMap();
        Map map = getCount(file, hm);

        //System.out.println(map);
        for(Map.Entry<String, Integer>entry:hm.entrySet()){
            System.out.println("文件类型:"+entry.getKey()+",个数"+entry.getValue());
        }
    }

    private static Map getCount(File file,HashMap hm) {
        File[] files = file.listFiles();
        for (File fls : files) {
            if (fls.isFile()){
                String[] arr = fls.getName().split("\\.");
                if(arr.length==1) continue;
                String type = arr[arr.length-1];
                if(hm.containsKey(type)){
                    hm.put(type,(int) hm.get(type) + 1);
                }else{
                    hm.put(type,1);
                }
            }else{
                getCount(fls, hm);
            }
        }
        return hm;
    }
}
